﻿Imports System.IO
Imports System.Drawing.Imaging

Module mdlFunciones

    Public Function getInteCod(ByVal strTabla As String, ByVal strCampo As String, ByVal strCondicion As String, ByRef strId As String)
        Dim dstRecord As New DataSet
        Dim idReturn As String = ""

        dstRecord.Tables.Add(strTabla)
        mdlCRUD.sqlRecord(dstRecord, strTabla, "SELECT " & strCampo & " FROM " & strTabla & " WHERE " & strCondicion)

        If dstRecord.Tables(strTabla).Rows.Count > 0 Then
            Dim arrReferencia As Array = dstRecord.Tables(strTabla).Rows(0).ItemArray.ToArray
            strId = arrReferencia(0).ToString
            Return True
        End If

        Return False
    End Function

    Public Function Base64ToImage(ByVal base64Code As String) As Image 'Lee cadena Base64 para se mostrada como Imagen
        Dim imageBytes As Byte() = Convert.FromBase64String(base64Code)
        Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
        Dim tmpImage As Image = Image.FromStream(ms, True)
        Return tmpImage
    End Function

    Public Sub grabaBase64ToImage(ByVal base64Code As String, ByVal strNombrearchivo As String, ByVal strRuta As String) 'Lee cadena Base64 graba como Imagen
        Dim imageBytes As Byte() = Convert.FromBase64String(base64Code)
        Dim fs As New FileStream(strRuta & "\" & strNombrearchivo, FileMode.CreateNew)
        fs.Write(imageBytes, 0, imageBytes.Length)
        fs.Close()
    End Sub

    Public Sub setListas(ByVal cmbLista As ComboBox, ByVal strSql As String, ByVal strSelvalor As String)
        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("Tabla")

        mdlCRUD.sqlRecord(dstRecord, "Tabla", strSql)

        cmbLista.DataSource = dstRecord.Tables("Tabla")
        cmbLista.DisplayMember = dstRecord.Tables("Tabla").Columns(1).Caption.ToString
        cmbLista.ValueMember = dstRecord.Tables("Tabla").Columns(0).Caption.ToString
        cmbLista.SelectedValue = strSelvalor
    End Sub

    Public Function getReferencia(ByVal strTabla As String, ByVal strCampo As String, ByVal strId As String, ByVal intIndex As Integer)
        Dim dstRecord As New DataSet
        Dim arrRow As Array
        dstRecord.Tables.Add(strTabla)

        'Cosulta a referencia
        mdlCRUD.loadRecord(dstRecord, strTabla, strTabla, strCampo & " = '" & strId & "'", Nothing)
        arrRow = dstRecord.Tables(strTabla).Rows(0).ItemArray.ToArray

        Return arrRow(intIndex).ToString

    End Function

    Public Function getEdad(ByVal strFechanacimiento As Object) As Integer
        Dim Años As Object
        ' comprueba si el valor no es nulo
        If strFechanacimiento = Nothing Then
            getEdad = 0
            Exit Function
        End If

        Años = DateDiff("yyyy", strFechanacimiento, Now)

        If Now < DateSerial(Year(Now), Month(strFechanacimiento), DateAndTime.Day(strFechanacimiento)) Then
            Años -= 1
        End If

        getEdad = CInt(Años)
    End Function

    'Generate new image dimensions
    Public Function GenerateImageDimensions(ByVal currW As Integer, ByVal currH As Integer, ByVal destW As Integer, ByVal destH As Integer) As Size
        'double to hold the final multiplier to use when scaling the image
        Dim multiplier As Double = 0

        'string for holding layout
        Dim layout As String

        'determine if it's Portrait or Landscape
        If currH > currW Then
            layout = "portrait"
        Else
            layout = "landscape"
        End If

        Select Case layout.ToLower()
            Case "portrait"
                'calculate multiplier on heights
                If destH > destW Then
                    multiplier = CDbl(destW) / CDbl(currW)
                Else

                    multiplier = CDbl(destH) / CDbl(currH)
                End If
                Exit Select
            Case "landscape"
                'calculate multiplier on widths
                If destH > destW Then
                    multiplier = CDbl(destW) / CDbl(currW)
                Else

                    multiplier = CDbl(destH) / CDbl(currH)
                End If
                Exit Select
        End Select

        'return the new image dimensions
        Return New Size(CInt((currW * multiplier)), CInt((currH * multiplier)))
    End Function

    'Resize the image
    Public Sub SetImage(ByVal pb As PictureBox)
        Try
            'create a temp image
            Dim img As Image = pb.Image

            'calculate the size of the image
            Dim imgSize As Size = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height)

            'create a new Bitmap with the proper dimensions
            Dim finalImg As New Bitmap(img, imgSize.Width, imgSize.Height)

            'create a new Graphics object from the image
            Dim gfx As Graphics = Graphics.FromImage(img)

            'clean up the image (take care of any image loss from resizing)
            gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

            'empty the PictureBox
            pb.Image = Nothing

            'center the new image
            pb.SizeMode = PictureBoxSizeMode.CenterImage

            'set the new image
            pb.Image = finalImg
        Catch e As System.Exception
            MessageBox.Show(e.Message)
        End Try
    End Sub
End Module
